﻿using Newtonsoft.Json;
using Prism.Mvvm;
using System.IO;

namespace IceCreamMachine.Models
{
    class RawMaterial: BindableBase
    {
        private int _strawberry = 100;
        public int Strawberry
        {
            get { return _strawberry; }
            set { SetProperty(ref _strawberry, value); }
        }

        private int _chocolates = 100;
        public int Chocolates
        {
            get { return _chocolates; }
            set { SetProperty(ref _chocolates, value); }
        }

        private int _iceCream = 100;
        public int IceCream
        {
            get { return _iceCream; }
            set { SetProperty(ref _iceCream, value); }
        }

        public RawMaterial() {
            
        }

        public RawMaterial(bool load)
        {
            if (load)
                Load();

        }

        public void Load()
        {
            var path = $@"{System.AppDomain.CurrentDomain.BaseDirectory}\data.json";

            if (File.Exists(path))
            {
                var json = File.ReadAllText(path);
                var entity = JsonConvert.DeserializeObject<RawMaterial>(json);
                Chocolates = entity.Chocolates;
                Strawberry = entity.Strawberry;
                IceCream = entity.IceCream;
               
            }
        }

        /// <summary>
        /// 持久化
        /// </summary>
        public void Save() {
            var json=JsonConvert.SerializeObject(this);
            var path = $@"{System.AppDomain.CurrentDomain.BaseDirectory}\data.json";
            if (File.Exists(path))
            {
                File.WriteAllText(path,json);
            }
            else
            {
                File.Create(path);
                File.WriteAllText(path, json);
            }
            
        }
    }
}
